package com.product.android.ui.widget;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.common.android.utils.NetWorkUtils;
import com.common.android.utils.ToastUtils;
import com.common.android.utils.concurrent.NdTinyHttpAsyncTask;
import com.nd.android.u.allcommon.R;
import com.product.android.commonInterface.BaseCommonStruct;
import com.product.android.commonInterface.CIConst;
import com.product.android.commonInterface.CommonInterfaceManager;
import com.product.android.commonInterface.contact.OapUserSimple;

/**
 * 在组织搜索人员时，用于触发从服务端搜索的界面项
 * 
 * @author lfa
 * 
 */
public class SearchTipItem extends RelativeLayout {

	/**
	 * 从服务端搜索完成后，回传搜索结果
	 * 
	 * @author lfa
	 * 
	 */
	public interface OnNetSearch {
		/**
		 * 回传搜索结果
		 * 
		 * @param list
		 *            从服务端搜索到的列表，不为null
		 */
		void onSearch(List<OapUserSimple> list);
	}

	/**
	 * 获取搜索文本接口
	 * 
	 * @author lfa
	 * 
	 */
	public interface OnGetSearchText {
		/**
		 * 获取搜索文本
		 * 
		 * @return 搜索文本
		 */
		String getSearchText();
	}

	private OnGetSearchText mGetSearchText; // 获取搜索文本的接口
	private Activity mActivity; // 所属Activity
	private OnNetSearch mNetSearch; // 调用服务端接口搜索用户后的通知接口

	private View mProgressBar; // 表示正在从服务端获取搜索结果的进度控件
	private View mViewSearchFailedTip; // 搜索失败时提示用户

	/**
	 * 构造函数
	 * 
	 * @param context
	 *            上下文
	 */
	public SearchTipItem(Context context) {
		super(context);
		initView();
	}

	/**
	 * 初始化界面
	 */
	private void initView() {
		final Context ctx = getContext();
		View rootView = LayoutInflater.from(ctx).inflate(
				R.layout.search_tip_item, this, true);
		rootView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				if (!NetWorkUtils.JudgeNetWorkStatus(ctx)) {
					ToastUtils.display(ctx, "没有可用网络");
					return;
				}
				if (isSearching()) {
					return; // 避免重复搜索
				}
				new SearchFromServer().execute(mGetSearchText.getSearchText());
			}
		});

		mProgressBar = rootView.findViewById(R.id.pb);
		mViewSearchFailedTip = rootView.findViewById(R.id.search_failed_tip);
	}

	/**
	 * 更新搜索信息，设置搜索完成后的回调函数
	 * 
	 * @param activity
	 *            所属activity
	 * @param searchText
	 *            要搜索的文本
	 * @param showNetSearchFailedTip
	 *            值为true时显示“抱歉，服务端暂无相匹配的信息”的提示，false时不显示
	 * @param netSearch
	 *            搜索结果回传接口
	 */
	public final void updateContent(final Activity activity,
			final OnGetSearchText getSearchText,
			final boolean showNetSearchFailedTip, final OnNetSearch netSearch) {
		mActivity = activity;
		mGetSearchText = getSearchText;
		mNetSearch = netSearch;

		if (mViewSearchFailedTip != null) {
			int visibility;
			if (showNetSearchFailedTip) {
				visibility = View.VISIBLE;
			} else {
				visibility = View.INVISIBLE;
			}
			mViewSearchFailedTip.setVisibility(visibility);
		}

	}

	/**
	 * 获取搜索状态
	 * 
	 * @return true 表示正在搜索
	 */
	private boolean isSearching() {
		return mProgressBar.isShown();
	}

	/**
	 * 执行从服务端搜索的任务类
	 * 
	 * @author lfa
	 * 
	 */
	private class SearchFromServer extends
			NdTinyHttpAsyncTask<String, Void, List<OapUserSimple>> {

		@Override
		protected void onPreExecute() {
			mProgressBar.setVisibility(View.VISIBLE);
			mViewSearchFailedTip.setVisibility(View.INVISIBLE);
		}

		@Override
		protected List<OapUserSimple> doInBackground(final String... params) {
			return netSearchFriend(params[0]);
		}

		/**
		 * 执行从服务端搜索的方法
		 * 
		 * @param searchText
		 *            要搜索的关键字
		 * @return 搜索结果列表
		 */
		@SuppressWarnings("unchecked")
		private List<OapUserSimple> netSearchFriend(final String searchText) {
			BaseCommonStruct param = new BaseCommonStruct();
			param.sPara = searchText;
			int iret = CommonInterfaceManager.INSTANCE.CommonInterfaceID(
					CIConst.CONTACT_MODELID,
					CIConst.CONTACT_NET_SEARCH_FRIEND_11014, param);
			List<OapUserSimple> list = null;
			if (iret == CIConst.COM_RET_OK) {
				list = (List<OapUserSimple>) param.obj2;
			}
			if (list == null) {
				return new ArrayList<OapUserSimple>();
			} else {
				return list;
			}
		}

		@Override
		protected void onPostExecute(final List<OapUserSimple> result) {
			if (mActivity.isFinishing()) {
				return;
			}
			mProgressBar.setVisibility(View.GONE);
			if (result.isEmpty()) { // 没搜索到
				mViewSearchFailedTip.setVisibility(View.VISIBLE);
			} else {
				if (mNetSearch != null) {
					mNetSearch.onSearch(result); // 回传搜索结果
				}
			}
		}
	}

}
